var cur_contest = cur_contest_detail;//cur_contest_detail 是一个全局变量在top_head tpl中定义的

navbarApp.controller('contest_file_list_controller', function ($scope, $http) {

    //$scope.getContestDetail = function (callBack) {
    //    $http.post("/contests/contest/detail", {contestId: $.getUrlParam('contestId')}).success(function (response) {
    //        if (response.status == '200') {
    //            cur_contest = response.data;
    //            $scope.cur_contest_detail = response.data;
    //            console.log(cur_contest);
    //            if(callBack && typeof callBack == 'function') {
    //                callBack();
    //            }
    //
    //        } else {
    //            layer.msg('服务器错误', {
    //                icon: 2,
    //                time: 2000 //2秒关闭（如果不配置，默认是3秒）
    //            }, function () {
    //                //do something
    //
    //            });
    //        }
    //    }).error(function (response) {
    //        layer.msg(response, {
    //            icon: 1,
    //            time: 2000 //2秒关闭（如果不配置，默认是3秒）
    //        }, function () {
    //            //do something
    //
    //        });
    //    })
    //}


    var initFileTable = function () {
        $('#dataTable').dataTable({
            language: {
                "sProcessing": "处理中...",
                "sLengthMenu": "显示 _MENU_ 项结果",
                "sZeroRecords": "没有匹配结果",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
                "sInfoPostFix": "",
                "sSearch": "搜索:",
                "sUrl": "",
                "sEmptyTable": "表中数据为空",
                "sLoadingRecords": "载入中...",
                "sInfoThousands": ",",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                },
                "oAria": {
                    "sSortAscending": ": 以升序排列此列",
                    "sSortDescending": ": 以降序排列此列"
                }
            },
            /*aoColumns:[ //设定各列宽度
             {"sWidth": "10%"},
             {"sWidth": "30%"},
             {"sWidth": "30%"},
             {"sWidth": "30%"}
             ],
             <th>大赛名称</th>
             <th>创建时间</th>
             <th>作品数量</th>
             <!--<th>参赛人数</th>-->
             <th>当前状态</th>
             <th>推荐</th>
             <th>权重</th>
             <th>管理</th>*/
            columns: [
                {
                    title: '文件/文件夹', data: 'name', "mRender": function (data, type, full) {
                    var contestLink = '';
                    contestLink = '/contests/contest/' + cur_contest.refer + '/' + data + "?contestId=" + cur_contest.id;
                    var rHtml = '<svg aria-hidden="true" class="octicon octicon-file-text" height="16" version="1.1" viewBox="0 0 12 16" width="12"><path d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg>';
                    if(!full.isFile) {
                        rHtml = '<svg aria-hidden="true" class="octicon octicon-file-directory" height="16" version="1.1" viewBox="0 0 14 16" width="14"><path fill-rule="evenodd" d="M13 4H7V3c0-.66-.31-1-1-1H1c-.55 0-1 .45-1 1v10c0 .55.45 1 1 1h12c.55 0 1-.45 1-1V5c0-.55-.45-1-1-1zM6 4H1V3h5v1z"></path></svg>';
                    }
                    rHtml = rHtml + '<a href="' + contestLink + '" target="_blank" ">' + data + '</a>';
                    return rHtml;
                }
                },
                //返回的size大小是字节, 需要格式化一下
                {
                    title: '大小', data: 'size', "mRender": function (data, type, full) {
                    return data ? sjApp.formatFileSize(data) : 0;
                }
                },
                {
                    title: '修改时间',
                    data: 'modifyTime',
                    "mRender": function (data, type, full) {
                        if (data) {
                            return new Date(data).pattern('yyyy-MM-dd HH:mm:ss');
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    title: '配置',
                    "data": null,
                    "orderable": false,
                    //"defaultContent": "<button>删除</button>&nbsp;<button>编辑</button>",
                    createdCell: function (td, cellData, rowData, row, col) {
                        // $(td).html('<a ng-click="showMe()" class="btn btn-danger btn-xs btn-line">删除</a>&nbsp;&nbsp;');
                        $(td).html('');
                        //删除暂时没有
                        //$(td).html('<a onclick="delete_contest(\'' + rowData.id + '\')" class="btn btn-danger btn-xs btn-line">删除</a>&nbsp;&nbsp;');
                        //var editEl = $('<a href="/contests/contest/add?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">基本配置</a>&nbsp;&nbsp;');
                        //var fileList = $('<a href="/contests/contest/file_list?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">文件列表</a>');
                        //
                        //
                        //$(td).append(editEl);
                        //$(td).append('&nbsp;&nbsp');
                        //$(td).append(fileList);
                        //$(td).append('&nbsp;&nbsp');


                        /*editEl.click(function(){
                         var rowD = rowData;
                         return function(){
                         $scope.edit_role(rowD);
                         }
                         }());*/
                        //$(td).append('&nbsp;<a href="/community/contest/config/review?id='+rowData.id+'" class="btn btn-metis-6 btn-xs btn-line">评审</a>');
                    }
                }
            ],
            "bSort": false,
            "bProcessing": true, //加载数据时显示正在加载信息
            "bServerSide": true, //指定从服务器端获取数据
            "bFilter": false, //不使用过滤功能
            "bLengthChange": false, //用户不可改变每页显示数量
            "iDisplayLength": 16, //每页显示10条数据
            "sAjaxSource": '/contests/contest/get_file_list',
            "fnServerParams": function (aoData) {
                /*aoData.push(
                 { "name": "roleName", "value": $("#role-name-search").val() }
                 );*/
            },
            "fnServerData": function (sSource, aoData, fnCallback) {
                var pageSize = aoData[4].value;
                var pageIndex = aoData[3].value / pageSize + 1;

                $.ajax({
                    type: "post",
                    dataType: 'json', //接受数据格式
                    cache: false,
                    data: {
                        path: cur_contest.refer
                    },
                    url: sSource,
                    success: function (data, textStatus) {
                        var r = {
                            iTotalRecords: 50,
                            iTotalDisplayRecords: 50,
                            aaData: []
                        };
                        if (data instanceof Array) {
                            r.aaData = data;
                        } else {
                            // var t ={};
                            // t.aaData = data.data;
                            // t.iTotalDisplayRecords = data.iTotalRecords;
                            // r =t;
                            data.aaData = data.data.items;
                            data.iTotalDisplayRecords = data.data.totalCount;
                            r = data;
                        }
                        fnCallback(r);
                    },
                    error: function () {
                        //请求出错处理
                        // alert("error");
                    }
                });

            }, //获取数据的处理函数
            "sPaginationType": "full_numbers", //翻页界面类型
            "headerClickable": false,
            "sortable": false/*,
             data: cur_result ||[{
             title: 'aa',
             createdTime: 'bb',
             itemCount: 3,
             state: 1,
             recommendation: 3,
             rating: 0
             }]*/
//         "sDom": "<'pull-right'l>t<'row'<'col-lg-6'f><'col-lg-6'p>>",
//         "sPaginationType": "bootstrap",
//         "oLanguage": {
//             "sLengthMenu": "Show _MENU_ entries"
//         }
        });
    }

    $scope.htmldecode = sjApp.htmldecode;

    $scope.cur_contest_detail = cur_contest;

    initFileTable();


});